package com.swz.dcrm.ui.home;


import android.annotation.SuppressLint;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.navigation.fragment.NavHostFragment;

import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.swz.dcrm.model.BaseResponse;
import com.swz.dcrm.model.Boutique;
import com.swz.dcrm.model.CarModel;

import com.swz.dcrm.model.Insurance;
import com.swz.dcrm.model.Quotation;
import com.swz.dcrm.model.QuotationBoutique;
import com.swz.dcrm.model.QuotationInsurance;
import com.swz.dcrm.ui.base.BaseFragment;
import com.swz.dcrm.R;

import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.http.Body;

import com.swz.dcrm.R;
import com.swz.dcrm.ui.car.CarBrandFragmentArgs;
import com.swz.dcrm.ui.mine.EditInfoFragmentArgs;
import com.swz.dcrm.ui.viewmodel.CarBrandViewModel;
import com.swz.dcrm.ui.viewmodel.CarManagementViewModel;
import com.swz.dcrm.ui.viewmodel.MainViewModel;
import com.swz.dcrm.util.Tool;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * A simple {@link Fragment} subclass.
 */
public class FeeInTotalFragment extends BaseFragment {

    @BindView(R.id.toolbar_title)
    TextView title;
    @BindView(R.id.tv_brand_name)
    TextView tvBrandName;
    @BindView(R.id.tv_model)
    TextView tvModel;

    @Inject
    CarManagementViewModel carManagementViewModel;
    @Inject
    CarBrandViewModel carBrandViewModel;
    @Inject
    MainViewModel mainViewModel;

    @BindView(R.id.tv_car_price)
    TextView tvCarPrice;
    @BindView(R.id.tv_acquisition_tax)
    TextView tvAcquisitionTax;
    @BindView(R.id.tv_boutique_fee)
    TextView tvBoutiqueFee;
    @BindView(R.id.tv_insurance_fee)
    TextView tvInsuranceFee;
    @BindView(R.id.tv_loan_fee)
    TextView tvLoanFee;
    @BindView(R.id.tv_maintenance_fee)
    TextView tvMaintenanceFee;
    @BindView(R.id.tv_other_fee)
    TextView tvOtherFee;
    @BindView(R.id.tv_license_plate_fee)
    TextView tvLicensePlateFee;
    @BindView(R.id.tv_total_fee)
    TextView tvTotalFee;


    private CarModel mCarModel;
    private Quotation quotation=new Quotation();
    public static FeeInTotalFragment newInstance() {

        return new FeeInTotalFragment();
        // Required empty public constructor
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onLoadRetry() {


    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        onLoadRetry();
    }

    @Override
    public int layoutId() {
        return R.layout.fragment_fee_in_total;
    }

    @Override
    public boolean initView() {
        getDigger().inject(this);
        title.setText(getString(R.string.feel_in_total_title));
        carBrandViewModel.getSelectedCarSeries().observe(this, carSeries -> {
            if (carSeries != null) {
                quotation.setCarSeriesId(carSeries.getId());
                quotation.setCarSeriesName(carSeries.getName());
                tvBrandName.setText(carBrandViewModel.getSelectedCarBrand().getValue().getName() +
                        carSeries.getName());
            }
        });
        mainViewModel.getEdit().observe(this,edit -> {
            if(edit==null||TextUtils.isEmpty(edit.getValue())){
                return;
            }
            if(edit.getTitle().equals(getString(R.string.feel_in_total_price_tag1))){
                tvCarPrice.setText(edit.getValue());
                quotation.setCarPrice(Double.valueOf(edit.getValue()));
            }else if(edit.getTitle().equals(getString(R.string.feel_in_total_price_tag2))){
                tvAcquisitionTax.setText(edit.getValue());
                quotation.setAcquisitionTax(Double.valueOf(edit.getValue()));
            }else if(edit.getTitle().equals(getString(R.string.feel_in_total_price_tag3))){
                tvLicensePlateFee.setText(edit.getValue());
                quotation.setLicensePlateFee(Double.valueOf(edit.getValue()));
            }else if(edit.getTitle().equals(getString(R.string.feel_in_total_price_tag5))){
                tvLoanFee.setText(edit.getValue());
                quotation.setLoanFee(Double.valueOf(edit.getValue()));
            }else if(edit.getTitle().equals(getString(R.string.feel_in_total_price_tag7))){
                tvMaintenanceFee.setText(edit.getValue());
                quotation.setMaintenanceFee(Double.valueOf(edit.getValue()));
            }else if(edit.getTitle().equals(getString(R.string.feel_in_total_price_tag8))){
                tvOtherFee.setText(edit.getValue());
                quotation.setOtherFee(Double.valueOf(edit.getValue()));
            }
            BigDecimal bigDecimal = new BigDecimal(tvCarPrice.getText().toString());
            bigDecimal = bigDecimal.add(new BigDecimal(tvAcquisitionTax.getText().toString()));
            bigDecimal = bigDecimal.add(new BigDecimal(tvLicensePlateFee.getText().toString()));
            bigDecimal = bigDecimal.add(new BigDecimal(tvLoanFee.getText().toString()));
            bigDecimal = bigDecimal.add(new BigDecimal(tvMaintenanceFee.getText().toString()));
            bigDecimal = bigDecimal.add(new BigDecimal(tvOtherFee.getText().toString()));
            bigDecimal = bigDecimal.add(new BigDecimal(tvBoutiqueFee.getText().toString()));
            bigDecimal = bigDecimal.add(new BigDecimal(tvInsuranceFee.getText().toString()));
            tvTotalFee.setText(bigDecimal.toString());
        });

        carBrandViewModel.getSelectedCarModel().observe(this, carModel -> {
            if (carModel != null) {
                quotation.setCarModelId(carModel.getId());
                quotation.setCarModelName(carModel.getName());
                tvModel.setText(carModel.getName());

                carManagementViewModel.getCarModelDetail(carModel.getId())
                        .observe(this, new Observer<BaseResponse<CarModel>>() {
                            @Override
                            public void onChanged(BaseResponse<CarModel> carModelBaseResponse) {
                                if(carModelBaseResponse==null){
                                    return;
                                }
                                if(carModelBaseResponse.isSuccess()){
                                    carManagementViewModel.getCarModelDetail().removeObserver(this);
                                    mCarModel = carModelBaseResponse.getData();
                                    tvCarPrice.setText(Tool.formatToNumber(new BigDecimal(mCarModel.getCarPrice())));
                                    tvAcquisitionTax.setText(Tool.formatToNumber(new BigDecimal(mCarModel.getAcquisitionTax())));
                                    tvLoanFee.setText(Tool.formatToNumber(new BigDecimal(mCarModel.getLoanFee())));
                                    tvLicensePlateFee.setText(Tool.formatToNumber(new BigDecimal(mCarModel.getLicensePlateFee())));
                                    tvMaintenanceFee.setText(Tool.formatToNumber(new BigDecimal(mCarModel.getMaintenanceFee())));
                                    tvOtherFee.setText(Tool.formatToNumber(new BigDecimal(mCarModel.getOtherFee())));
                                    //todo 报价单
                                    quotation.setAcquisitionTax(mCarModel.getAcquisitionTax());
                                    quotation.setCarPrice(mCarModel.getCarPrice());
                                    quotation.setLicensePlateFee(mCarModel.getLicensePlateFee());
                                    quotation.setOtherFee(mCarModel.getOtherFee());
                                    quotation.setLoanFee(mCarModel.getLoanFee());
                                    quotation.setMadoubleenanceFee(mCarModel.getMaintenanceFee());
                                    BigDecimal bigDecimal = new BigDecimal(mCarModel.getCarPrice());
                                    bigDecimal = bigDecimal.add(new BigDecimal(mCarModel.getAcquisitionTax()));
                                    bigDecimal = bigDecimal.add(new BigDecimal(mCarModel.getLoanFee()));
                                    bigDecimal = bigDecimal.add(new BigDecimal(mCarModel.getLicensePlateFee()));
                                    bigDecimal = bigDecimal.add(new BigDecimal(mCarModel.getMaintenanceFee()));
                                    bigDecimal = bigDecimal.add(new BigDecimal(tvBoutiqueFee.getText().toString()));
                                    bigDecimal = bigDecimal.add(new BigDecimal(tvInsuranceFee.getText().toString()));
                                    tvTotalFee.setText(bigDecimal.doubleValue() + "");
                                }else {
                                    showToast(carModelBaseResponse.getMsg());
                                }


                            }
                        });
            }
        });
        carManagementViewModel.getSelectedInsurance().observe(this,insurances -> {
            BigDecimal bigDecimal = new BigDecimal(tvCarPrice.getText().toString());
            bigDecimal = bigDecimal.add(new BigDecimal(tvAcquisitionTax.getText().toString()));
            bigDecimal = bigDecimal.add(new BigDecimal(tvLicensePlateFee.getText().toString()));
            bigDecimal = bigDecimal.add(new BigDecimal(tvLoanFee.getText().toString()));
            bigDecimal = bigDecimal.add(new BigDecimal(tvMaintenanceFee.getText().toString()));
            bigDecimal = bigDecimal.add(new BigDecimal(tvOtherFee.getText().toString()));
            bigDecimal = bigDecimal.add(new BigDecimal(tvBoutiqueFee.getText().toString()));
            List<QuotationInsurance>list;
            if (insurances != null && insurances.size() > 0) {
                list=new ArrayList<>();
                BigDecimal boutiquePrices=null;
                for (Insurance insurance : insurances) {
                    QuotationInsurance quotationInsurance=new QuotationInsurance();
                    quotationInsurance.setCarModelId(insurance.getCarModelId());
                    quotationInsurance.setPrice(Double.valueOf(insurance.getPrice()));
                    quotationInsurance.setInsuranceConfigId(insurance.getId());
                    list.add(quotationInsurance);
                    if (insurance.getPrice() == null) {
                        continue;
                    }
                    if(boutiquePrices==null){
                        boutiquePrices=new BigDecimal(insurance.getPrice());
                    }else {
                        boutiquePrices=boutiquePrices.add(new BigDecimal(insurance.getPrice()));
                    }
                    bigDecimal = bigDecimal.add(new BigDecimal(insurance.getPrice()));
                }
                quotation.setQuotationInsuranceList(list);
                quotation.setInsurancePrice(boutiquePrices.doubleValue());
                tvInsuranceFee.setText(boutiquePrices.toString());
            }else {
                quotation.setQuotationInsuranceList(null);
                quotation.setInsurancePrice(0);
                tvInsuranceFee.setText("0");
            }



            quotation.setNormalCarFullPrice(bigDecimal.doubleValue());
            tvTotalFee.setText(bigDecimal.toString());
        });
        carManagementViewModel.getSelectedBoutique().observe(this, boutiques -> {
            BigDecimal bigDecimal = new BigDecimal(tvCarPrice.getText().toString());
            bigDecimal = bigDecimal.add(new BigDecimal(tvAcquisitionTax.getText().toString()));
            bigDecimal = bigDecimal.add(new BigDecimal(tvLicensePlateFee.getText().toString()));
            bigDecimal = bigDecimal.add(new BigDecimal(tvLoanFee.getText().toString()));
            bigDecimal = bigDecimal.add(new BigDecimal(tvMaintenanceFee.getText().toString()));
            bigDecimal = bigDecimal.add(new BigDecimal(tvOtherFee.getText().toString()));
            bigDecimal = bigDecimal.add(new BigDecimal(tvInsuranceFee.getText().toString()));
            if (boutiques != null && boutiques.size() > 0) {
                List<QuotationBoutique> list=new ArrayList<>();
                BigDecimal boutiquePrices=null;
                for (Boutique boutique : boutiques) {
                    QuotationBoutique quotationBoutique=new QuotationBoutique();
                    quotationBoutique.setBoutiqueId(boutique.getId());
                    quotationBoutique.setBoutiqueName(boutique.getBoutiqueName());
                    quotationBoutique.setPrice(Double.valueOf(boutique.getPrice()));
                    list.add(quotationBoutique);
                    if (boutique.getPrice() == null) {
                        continue;
                    }
                    if(boutiquePrices==null){
                        boutiquePrices=new BigDecimal(boutique.getPrice());
                    }else {
                        boutiquePrices=boutiquePrices.add(new BigDecimal(boutique.getPrice()));
                    }
                    bigDecimal = bigDecimal.add(new BigDecimal(boutique.getPrice()));
                }
                quotation.setQuotationBoutiqueList(list);
                quotation.setBoutiquePrice(boutiquePrices.doubleValue());
                tvBoutiqueFee.setText(boutiquePrices.toString());
            }else {
                quotation.setQuotationBoutiqueList(null);
                quotation.setBoutiquePrice(0);
                tvBoutiqueFee.setText("0");
            }

            quotation.setNormalCarFullPrice(bigDecimal.doubleValue());
            tvTotalFee.setText(bigDecimal.toString());
        });
        carManagementViewModel.getAddQuotation().observe(this,quotationBaseResponse -> {
            if(quotationBaseResponse.isSuccess()){
                go(null,R.id.action_feeInTotalFragment_to_quotationFragment,null);
            }else {
                showToast(quotationBaseResponse.getMsg());
            }

        });
        return true;
    }

    @OnClick({R.id.ll_car_price,R.id.ll_license_plate_fee,R.id.ll_maintenance_fee,R.id.ll_other_fee,R.id.ll_loan_fee,R.id.ll_acquisition_tax})
    public void edit(View view){
        EditInfoFragmentArgs.Builder builder=new EditInfoFragmentArgs.Builder();
        builder.setInputType(String.valueOf(8194));
        switch (view.getId()){
            case R.id.ll_car_price:
               builder.setTitle(getString(R.string.feel_in_total_price_tag1));
               builder.setValue(tvCarPrice.getText().toString());
               break;
            case R.id.ll_acquisition_tax:
                builder.setTitle(getString(R.string.feel_in_total_price_tag2));
                builder.setValue(tvAcquisitionTax.getText().toString());
                break;
            case R.id.ll_license_plate_fee:
                builder.setTitle(getString(R.string.feel_in_total_price_tag3));
                builder.setValue(tvLicensePlateFee.getText().toString());
                break;
            case R.id.ll_loan_fee:
                builder.setTitle(getString(R.string.feel_in_total_price_tag5));
                builder.setValue(tvLoanFee.getText().toString());
                break;
            case R.id.ll_maintenance_fee:
                builder.setTitle(getString(R.string.feel_in_total_price_tag7));
                builder.setValue(tvMaintenanceFee.getText().toString());
                break;
            case R.id.ll_other_fee:
                builder.setTitle(getString(R.string.feel_in_total_price_tag8));
                builder.setValue(tvOtherFee.getText().toString());
                break;
        }
        go(null,R.id.action_feeInTotalFragment_to_editInfoFragment,builder.build().toBundle());
    }

    @OnClick({R.id.ll_brand, R.id.ll_model, R.id.ll_boutique,R.id.ll_insurance_fee})
    public void selectCarBrand(View view) {
        switch (view.getId()) {
            case R.id.ll_brand:
                go(null, R.id.action_feeInTotalFragment_to_carBrandFragment, null);
                break;
            case R.id.ll_model:
                if (carBrandViewModel.getSelectedCarBrand().getValue() != null) {
                    CarBrandFragmentArgs.Builder builder = new CarBrandFragmentArgs.Builder();
                    builder.setBrand(new Gson().toJson(carBrandViewModel.getSelectedCarBrand().getValue()));
                    go(null, R.id.action_feeInTotalFragment_to_carBrandFragment, builder.build().toBundle());
                } else {
                    showToast("请先选择车型");
                }
                break;
            case R.id.ll_boutique:
                go(null, R.id.action_feeInTotalFragment_to_boutiqueFragment, null);
                break;
            case R.id.ll_insurance_fee:
                go(null, R.id.action_feeInTotalFragment_to_insuranceFragment, null);
                break;

        }

    }

    @Override
    public void onDestroyView() {
        mainViewModel.getEdit().setValue(null);
        super.onDestroyView();
    }

    @OnClick(R.id.iv_back)
    public void backClick() {
        NavHostFragment.findNavController(this).navigateUp();
    }
    @OnClick(R.id.tv_confirm)
    public void confirm(){
        carManagementViewModel.addQuotation(quotation);
    }
}
